/**
 *
 */
package jp.seraph.jsade.task;

import jp.seraph.jsade.action.AgentAction;
import jp.seraph.jsade.core.Player;
import jp.seraph.jsade.core.World;

/**
 * 実行TaskをTaskSelectorで決定するAgent実装
 *
 */
public class TaskSelectorAgent implements jp.seraph.jsade.core.Agent {
    public TaskSelectorAgent(TaskSelector aSelector) {
        mSelector = aSelector;
        mTaskAgent = new TaskAgent();
    }

    private TaskSelector mSelector;
    private TaskAgent mTaskAgent;

    /**
     *
     * @see jp.seraph.jsade.task.TaskAgent#think(jp.seraph.jsade.core.World,
     *      jp.seraph.jsade.core.Player)
     */
    public void think(World aWorld, Player aPlayer) {
        AgentTask tNewTask = mSelector.selectTask(aWorld);

        if (tNewTask != null) {
            if (mTaskAgent.getTaskCount() != 0) {
                AgentTask tCurrentTask = mTaskAgent.getTask(0);
                tCurrentTask.cancel();
            }

            mTaskAgent.InsertTask(tNewTask);
        }

        mTaskAgent.think(aWorld, aPlayer);

        AgentAction tAction = null;
        tAction = mSelector.getAgentAction(aWorld);

        if (tAction != null) {
            aPlayer.append(tAction.getActionEffector());
        }
    }

    public void setSelector(TaskSelector aSelector) {
        mSelector = aSelector;
    }
}
